/**
 * @class   wosh::services::CommunicationManagerBundle
 * @brief   Network-singleton service designed to manage and route messages
 *          between users (as the system itself).
 *
 *          Communication Manager manages the (updated) list of Communicator(s)
 *          and Interpreter(s) services.
 *
 *          WOSH system may have many wosh::Communicator services with different
 *          features and properties (time-dependent).
 *          There are also wosh::Interpreter services which convert/translate
 *          Notification format (language/type).
 *
 *          Moreover the service interacts with the wosh::UserManager core module
 *          reading and updating the Communication-related fields of each user.
 *          Each user holds information about used communicators, their
 *          timestamp and ranking.
 *
 *          Communication Manager manages collection of NotificationFlow.
 *          A wosh::communication::NotificationFlow contains the original
 *          Notification (implementation), evetually its translated version
 *          and some processing information.
 *
 *          Main goal is to select the (best) communication channel (Communicator) for a Notification.
 *          Notification may be interpreted (translated) within the flow.
 *          On sending acknoledgment, the flow is archived.
 *
 *          Communication Manager intercepts Notification(s) and creates/updates flows.
 *          Creating a new flow for a new Notification and appending translated version
 *          when it exists (and is under processing).
 *
 *          Processing is asynchronous, FIFO order (by internal queue).
 *
 *
 *
 *
 *
 *
 * @ingroup Services
 ****************************************************************************
 * @version $Id: CommunicationManagerBundle.h 2524 2010-05-22 06:05:19Z alex $
 * @author  Alessandro Polo
 ****************************************************************************/
/* Copyright (c) 2007-2010, WOSH - Wide Open Smart Home 
 * by Alessandro Polo - OpenSmartHome.com
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the OpenSmartHome.com WOSH nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY Alessandro Polo ''AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL Alessandro Polo BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 ****************************************************************************/

#ifndef __WOSH_Bundles_Services_CommunicationManager_H__
 #define __WOSH_Bundles_Services_CommunicationManager_H__

 #include <core/BundleGeneric.h>
 #include <core/Thread.h>

 #include <core/Notification.h>
 #include <core/Communicator.h>

 #include <interfaces/Service.h>
 #include <interfaces/services/CommunicationManager.h>
 #include <interfaces/services/CommunicatorService.h>
 #include <interfaces/services/InterpreterService.h>

 #include "ICommunicationManagerListener.h"
 
class CommunicationManagerParser;


namespace wosh {
 namespace services {

 #define _CommunicationManager_VERSION				0.25


class CommunicationManagerBundle :	public BundleGeneric,
									public virtual wosh::interfaces::services::CommunicationManager,
									public virtual ICommunicationManagerListener
 {
	WOSH_BUNDLE(wosh::services::CommunicationManagerBundle)
	WOSH_BUNDLE_INTERFACE("wosh::interfaces::services::CommunicationManager")
	WOSH_BUNDLE_SINGLETON(1,0)


	public:
		CommunicationManagerBundle();

		virtual ~CommunicationManagerBundle();


	public:
		WRESULT bundleStart();
		WRESULT bundleStop();

		void busMessage( const Message& message, const Bus* source );

		bool updatingProperty( bool& do_update, const Variant& value_proposed, Property& property_current, const PropertiesProvider* source );

//@}
/** @name CommunicationManager Interface for Communication callbacks
 * @{
 ******************************************************************************/
	public:

//@}
/** @name Internal methods
 * @{
 ******************************************************************************/
	protected:
		WRESULT updateCommunicators();
		WRESULT updateInterpreters();
//@}
	private:
		Response* mmDoListMessages( const Request* request );
		Response* mmDoListMessagesAr( const Request* request );
		Response* mmDoListMessagesMy( const Message* requestMessage );

		Response* mmDoMark( const Message* requestMessage );

		Response* mmDoTell( const Message* requestMessage );
		Response* mmDoFakeCommunicatorEvent( const Request* request );

		WRESULT mmDoSaveMessages();

	protected:
		CommunicationManagerImpl* commWorker;	///< real implementation
		CommunicationManagerParser* commParser;	///< real implementation


}; // class def

 }; // namespace services
}; // namespace wosh

#endif //__WOSH_Bundles_Services_CommunicationManager_H__
